<template>
  <div>
    <div class="add" v-if="state1" v-show="state">
      <el-button
        size="mini"
        type="primary"
        plain
        @click="add">添加管理员</el-button>
    </div>
    <!--    添加按钮弹出框-->
    <div v-if="!state1" class="add1">
      <el-form ref="form" :model="form1" label-width="80px" style="width: 300px">
        <el-form-item label="姓名" style="width: 200px">
          <el-input v-model="form1.name"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form1.username"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form1.phone"></el-input>
        </el-form-item>
        <el-form-item label="性别" style="width: 200px">
          <el-select v-model="form1.sex" placeholder="请选择性别">
            <el-option label="女" value="女"></el-option>
            <el-option label="男" value="男"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addAdmins">立即创建</el-button>
          <el-button @click="quxiao2">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div  v-show="state" v-if="state1">
      <el-table
        :data="tableData"
        style="width: 80%;margin-top: -50px;margin-bottom: 30px">
        <el-table-column
          align="center"
          label="用户名"
          width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="电话"
          width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.phone }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="用户类型"
          width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.type }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="状态"
          width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.state }}</span>
          </template>
        </el-table-column>
        <el-table-column  label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini"
                       @click="updateStatus(scope.$index, scope.row)">修改状态</el-button>
            <el-button size="mini"
                       type="danger"
                       @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pager :pageNo="searchParams.pageNo" :pageSize="searchParams.pageSize" :total=$store.state.backstage.total :continues="5" @getPageNo="getPageNo"></pager>
    <!--    弹出编辑框表单-->
    <div v-show="!state" class="add1">
      <el-form ref="form" :model="form" label-width="80px" style="width: 400px">
        <el-form-item label="姓名" style="width: 200px">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="性别" style="width: 200px">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="女" value="女"></el-option>
            <el-option label="男" value="男"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="adminUpdate">确定修改</el-button>
          <el-button @click="quxiao">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import pager from "../pager";

export default {
  name:'userModule',
  data() {
    return {
      state1:true,
      state:true,
      form: {
        id:'',
        username: '',
        name:'',
        phone:'',
        sex: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: '',
        state:false,
      },
      //  添加
      form1: {
        username: '',
        name:'',
        phone:'',
        sex: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: '',
        state:false,
      },
      searchParams:{
        pageNo:1,
        pageSize:3,
      },
      tableData: []
    }
  },
  components:{
    pager
  },
  mounted() {
    // 获取用户所有信息
    this.getData()
  },
  methods: {
    getPageNo(pageNo){
      this.searchParams.pageNo = pageNo
      //再次请求
      this.getData()
    },
    async getData(){
      await this.$store.dispatch('getBackstageUser',{page:this.searchParams.pageNo,pageSize:this.searchParams.pageSize})
      this.showUser()
    },
    //获取用书展示信息
    showUser(){
      this.tableData.splice(0,this.tableData.length)
      this.$store.state.backstage.records.forEach((item)=>{
        let {id,username,phone,name,status} = item
        let user = {
          id:id,
          phone:phone,
          name:username,
          type:name,
          state:status
        }
        this.tableData.push(user)
      })
    },
    //编辑按钮
    handleEdit(index, row) {
      console.log(index, row);
      this.state = false
      //清空输入框
      this.form = {name: '',username: '',phone: '',sex: ''}
      this.form.id = row.id
    },
    async handleDelete(index, row) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let id = row.id
        await this.$store.dispatch('deleteUser', {id:id})
        await this.getData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //新增员工
    async addAdmins() {
      let {name,username,phone,sex} = this.form1;
      (name && username && phone && sex)&& await this.$store.dispatch('addAdmin',{name,username,phone,sex})
      if (this.$store.state.backstage.addAdminInfo.code === 1){
        this.$message({
          message: this.$store.state.backstage.addAdminInfo.data,
          type: 'success'
        })
        this.quxiao2()
      }else {
        this.$message.error(this.$store.state.backstage.addAdminInfo.msg || '添加失败');
      }
    },
    quxiao(){
      this.state = true
    },
    add(){
      this.state1 = false
      //清空输入框
      this.form1 = {name: '',username: '',phone: '',sex: ''}
    },
    quxiao2(){
      this.state1 = true
    },
    //修改员工
    async adminUpdate(){
      let {id,name,username,phone,sex} = this.form;
      (name && username && phone && sex)&& await this.$store.dispatch('adminUpdate',{id,name,username,phone,sex})
      if (this.$store.state.backstage.updateUserInfo.code === 1){
        this.$message({
          message: this.$store.state.backstage.updateUserInfo.data,
          type: 'success'
        });
        this.quxiao()
        this.getData()
      }else {
        this.$message.error(this.$store.state.backstage.updateUserInfo.msg);
      }
    },
    //修改status
    async updateStatus(index,row){
      let status = row.state
      let id = row.id
      if (status === 1){
        status = 0
      }else {
        status = 1
      }
      await this.$store.dispatch('adminUpdate',{id,status})
      this.getData()
    }
  }
}
</script>
<style scoped>
.add{
  position: relative;
  top: 10px;
  left: 1200px;
  width: 100px;
  height: 50px;
  z-index: 1000;
}
.add1{
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
</style>
